<template>
    <div>
        <!-- 通过：绑定属性给子组件传值 -->
        <!-- <home-header :city="city"></home-header> -->
        <home-header></home-header>
        <home-swiper :list="swiperList"></home-swiper>
        <home-icons :iconslist="iconList"></home-icons>
        <home-recommend :recommendlist="recommendList"></home-recommend>
        <home-weekend :weekendlist="weekendList"></home-weekend>
    </div>
</template>

<script>
import HomeHeader from './components/Header'
import HomeSwiper from './components/Swiper'
import HomeIcons from './components/Icons'
import HomeRecommend from './components/Recommend'
import HomeWeekend from './components/Weekend'
import axios from 'axios'
import { mapState } from 'vuex'

export default {
    name:'Home',
    components:{
        HomeHeader,
        // HomeHeader: () => import('./components/Header'), //异步加载header
        HomeSwiper,
        HomeIcons,
        HomeRecommend,
        HomeWeekend
    },
    data() {
        return {
            // city:'', //存放城市
            lastCity:'', //存放上次请求的城市
            swiperList:[],  //存放轮播图地数组
            iconList:[],  //存放图标的数组
            recommendList:[], //存放热门推荐的数组
            weekendList:[] //存放周末去哪儿的数组
        }
    },
    created() {  //视频为在mounted里面加载，不合适，应该是created里面加载
        this.lastCity = this.city;
        this.getHomeInfo();
        // console.log('created')
    },
    computed: {
        ...mapState(['city'])
    },
    methods: {
        getHomeInfo() {  //获取ajax数据
            axios.get('/api/index.json?city='+ this.city)
            .then(this.getHomeInfoSucc)
        },
        getHomeInfoSucc(res) {
            console.log(res)
            res = res.data;
            if(res.ret && res.data){
                const data = res.data;
                // this.city = data.city;
                this.swiperList = data.swiperList;
                this.iconList = data.iconList;
                this.recommendList = data.recommendList;
                this.weekendList = data.weekendList;
            }
            
        }
    },
    activated() { //缓存以后，当页面重新显示的时候，会执行activated
        // console.log('activated')
        if(this.lastCity !== this.city){
            this.lastCity = this.city;
            this.getHomeInfo();
        }
    },
}
</script>

<style>

</style>